package com.gopay.common.cps.service.global.impl;

import com.gopay.common.cps.dao.global.GopayGlobalVariablesDao;
import com.gopay.common.cps.service.global.GopayGlobalVariablesService;
import com.gopay.common.domain.global.GopayGlobalVariables;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 国付宝系统全局变量处理类
 * @author LEI XIAOBAO
 * @date 2016年1月29日
 */
@Service("gopayGlobalVariablesService")
public class GopayGlobalVariablesServiceImpl implements GopayGlobalVariablesService{

	@Resource(name="gopayGlobalVariablesDao")
	private GopayGlobalVariablesDao gopayGlobalVariablesDao;
	
	@Override
	public String getGopayGlobalVariablesByParamKey(String paramKey) {
		if( StringUtils.isBlank(paramKey) ){
			return null;
		}
		return gopayGlobalVariablesDao.getGoPayGlobalVariablesByParamKey(paramKey);
	}

	@Override
	public List<?> getGopayGlobalVariablesListByCondition(Map<String, Object> params) {
		return gopayGlobalVariablesDao.getGopayGlobalVariablesListByCondition(params);
	}

	@Override
	public int queryGopayGlobalVariablesCount(Map<String, Object> paramMap) {
		return gopayGlobalVariablesDao.queryGopayGlobalVariablesCount(paramMap) ;
	}

	@Override
	public List<GopayGlobalVariables> queryGopayGlobalVariables(Map<String, Object> paramMap, Integer page, Integer rowNum) {
		List orderList  = gopayGlobalVariablesDao.queryGopayGlobalVariables(paramMap, page,rowNum);
		return orderList ;
	}

	@Override
	public GopayGlobalVariables getVariablesById(String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id",Integer.parseInt(id));
		List orderList = gopayGlobalVariablesDao.getGopayGlobalVariablesList(params);
		if(orderList !=null && orderList.size() >0){
			GopayGlobalVariables  var = (GopayGlobalVariables)orderList.get(0);
			return var;
		}
		return null;
	}

	@Override
	public void saveGlobalVariables(GopayGlobalVariables globalVariables) {
		gopayGlobalVariablesDao.save(globalVariables);
	}

	@Override
	public void editGlobalVariables(GopayGlobalVariables globalVariables) {
		gopayGlobalVariablesDao.update(globalVariables);
	}

	@Override
	public boolean checkRepeatParamKey(String paramKey,String keyId) {
		Map<String, Object> params = new HashMap<>();
		params.put("paramKey",paramKey);
		List orderList = gopayGlobalVariablesDao.getGopayGlobalVariablesList(params);
		GopayGlobalVariables  var = null;
		if(orderList !=null && orderList.size()>0){
			 var = (GopayGlobalVariables)orderList.get(0);
		}
		keyId = (keyId==null ? "" : keyId);
		if (var == null) {
			return false;
		}else {
			return !keyId.equals(var.getId().toString());
		}
	}

}
